/*******************************************************************************
 * Filename: league_home.js 
 *
 *
 * Author: Phil Peterson
 * Date: 10/25/2009
 *
 ******************************************************************************/

function postLoadLeague(){
	setMessage();
	collapseRows();
}

function scheduleLeague(){
    window.location = "schedule_generation.php";
}

function hoverButton(elem){
    elem.style.cursor = "pointer";
}

function showAll(elem){
	var rows = document.getElementsByClassName('message_row_hidden');

	if (elem.innerHTML == 'View All'){
		for (i = 0; i < rows.length; i++){
			rows[i].style.display = 'table-row';
		}
		elem.innerHTML = 'View Short List';
	}
	else {
		for (i = 0; i < rows.length; i++){
			rows[i].style.display = 'none';
		}

		elem.innerHTML = 'View All';
	}	
}

function setMessage(){
    document.getElementById("message_post").value = "What's on your mind?";
	//Check if need to show the view all button
	
	if (g_num_message > 5)
		document.getElementById("view_all").style.visibility = "visible";
	else
		document.getElementById("view_all").style.visibility = "hidden";
}

function messageFocus(elem){
    if (elem.value == "What's on your mind?")
        elem.value = "";

    document.getElementById("post_button").style.visibility = "visible";
    elem.style.color = "#000000";
}


function messageBlur(elem){
    if (elem.value == ""){
        elem.style.color = "#777777";
        elem.value = "What's on your mind?";
        document.getElementById("post_button").style.visibility = "hidden";
    }
}

function submitMessage(elem){

    var post = document.getElementById("message_post").value;
    //Check if the post is empty, and terminates the function is so
    if (post == "")
        return;
    else if (checkInput(post) == false){
        alert("Invalid characters in the post. Please try again");
        return;
    }
    else {
        //Setup a HTTP post request to add the post to the database
	    var type        = "messagePost";
	    var param_names = "type,teamId,post";
	    var param_vals  =  type + "," + g_team_id + "," + post;
	    var url         = "request_data.php";

	    var response = postRequest(param_names, param_vals, url);
    
		var post = document.getElementById("message_post");
		post.style.color = "#777777";
		post.value = "What's on your mind?";
		document.getElementById("post_button").style.visibility = "hidden";
		
		var table = document.getElementById("message_table");
		var rows = table.rows;
		var newRow = table.insertRow(1);
		
        var temp = response.split("|");
        post   = temp[0];
        date   = temp[1];
        time   = temp[2];
        t_name = temp[3];
        
        //Setup a HTTP post request to add the post to the database
	    var type        = "checkAvatar";
	    var param_names = "type,teamId";
	    var param_vals  =  type + "," + g_team_id;
	    var url         = "request_data.php";

	    var response = postRequest(param_names, param_vals, url);
	    
	    window.location.reload();
	    return;

		newRow.className = "message_row_show";
		newRow.innerHTML = "<td class='rela_row'> \
                                <div class='team_message'> \
                                    <img class='team_icon' src='images/icons/teams/" + response + " '/> \
                                    <span class='team_message_name'>" + t_name + "</span> \
                                    <span class='team_message_time'>" + date + " " + time + "</span> \
                                    <div class='team_message_msg'>" + post "</div> \
                                </div> \
                            </td>";
	
		//Check if the default message it there
		g_num_message++;

		rows = document.getElementById("message_table").rows;

		if (g_num_message == 1)
			rows = document.getElementById("message_table").deleteRow(2);
		

		if (g_num_message > 5){
			document.getElementById("view_all").style.visibility = "visible";
			rows = document.getElementById("message_table").rows;
			rows[6].className = "message_row_hidden";
		}
    }
}

